package com.htx.zqs.blesmartmask.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.htx.zqs.blesmartmask.R;


/**
 * Created by KomoriWu
 * on 2019/2/21.
 */


public class DialogUtils {

    private static Dialog sDialog;


    private static Dialog buildDialog(Context context, boolean cancleable, boolean outsideTouchable) {
        Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(cancleable);
        dialog.setCanceledOnTouchOutside(outsideTouchable);
        return dialog;
    }

    public static void setDialogStyle(Context activity, Dialog dialog, int measuredHeight) {
        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//todo keycode to show round corner
        WindowManager.LayoutParams wl = window.getAttributes();
        // 以下这两句是为了保证按钮可以水平满屏
        int width = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
        int height = (int) (((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight() * 0.9);
        wl.width = (int) (width * 1);  // todo keycode to keep gap
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;  //TODO  一般情况下为wrapcontent,最大值为height*0.9
        if (measuredHeight > height) {
            wl.height = height;
        }
        // 设置显示位置
        if (!(activity instanceof Activity)) {
            wl.type = WindowManager.LayoutParams.TYPE_TOAST;//todo keycode to improve window level
        }
        dialog.onWindowAttributesChanged(wl);
    }

    public static Dialog showBaseViewDialog(Context context, View view) {
        dismiss();
        sDialog = new Dialog(context, R.style.CustomDialog);
        sDialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        sDialog.setCancelable(true);
        sDialog.show();
        return sDialog;
    }

    public static Dialog showBaseViewDialog(Context context, View view, boolean iscanCancel) {
        dismiss();
        sDialog = new Dialog(context, R.style.CustomDialog);
        sDialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        sDialog.setCancelable(iscanCancel);
        sDialog.show();
        return sDialog;
    }


    public interface OnSureClickListener {
        void onSure();
    }


    public static void dismiss() {
        try {
            if ((sDialog != null) && sDialog.isShowing()) {
                sDialog.dismiss();
            }
        } catch (final IllegalArgumentException e) {
            // Handle or log or ignore
        } catch (final Exception e) {
            // Handle or log or ignore
        } finally {
            sDialog = null;
        }

    }


    public static void showBottomDialog(Context context, View view) {
        dismiss();
        sDialog = new Dialog(context);
        sDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        sDialog.setCancelable(true);
        sDialog.setCanceledOnTouchOutside(true);
        sDialog.setContentView(view);

        Window window = sDialog.getWindow();
        assert window != null;
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.mystyle);
        setDialogStyle(context, sDialog, 0);
        sDialog.show();
    }

}
